package project.dade.cn.bst.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;
import cn.bingoogolapple.androidcommon.adapter.BGAOnRVItemClickListener;
import project.dade.cn.bst.R;
import project.dade.cn.bst.base.ZJPBaseBarActivity;
import project.dade.cn.bst.fragments.PageFragment;
import project.dade.cn.bst.manager.ApiConfig;
import project.dade.cn.bst.model.BaseRequestsModel;
import project.dade.cn.bst.model.GoshopingModel;
import project.dade.cn.bst.network.callback.ResultCallback;
import project.dade.cn.bst.network.request.OkHttpRequest;
import project.dade.cn.bst.utils.AppUtil;
import project.dade.cn.bst.utils.LogUtil;
import project.dade.cn.bst.utils.NetRequestUtil;
import project.dade.cn.bst.utils.ToastUtil;
import project.dade.cn.bst.views.EmptyLayout;

/**
 * Created by vain zhou on 2015/11/10.
 */
public class GoshoppingActivity extends ZJPBaseBarActivity implements View.OnClickListener,  BGAOnRVItemClickListener {
    @InjectView(R.id.errorLayout)
    EmptyLayout mErrorLayout;
    @InjectView(R.id.tabs)
    TabLayout tabLayout;
    @InjectView(R.id.viewpager)
    ViewPager viewPager;
    private MyFragmentPagerAdapter adapter;
    private List<Fragment> listFragment = new ArrayList<Fragment>();
    List<GoshopingModel> Dates = new ArrayList<GoshopingModel>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_investmant);
        adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),
                this);
        viewPager.setAdapter(adapter);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        requestGetclass();
    }

    public void onResume() {
        super.onResume();
        mErrorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
        initRequest();

    }


    private void initRequest() {
        if (AppUtil.isNetworkAvailable(GoshoppingActivity.this)) {

        } else {
            mErrorLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
            ToastUtil.showShort(GoshoppingActivity.this, R.string.tips_network_error);


        }
    }

    private void requestGetclass() {
        new OkHttpRequest.Builder()
                .url(ApiConfig.getHostAddress() + ApiConfig.SERVER_URL.Class)
                .get(new ResultCallback<String>() {
                    @Override
                    public void onError(com.squareup.okhttp.Request request, Exception e) {
                        mErrorLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                        ToastUtil.showShort(GoshoppingActivity.this, "网络相应失败请稍后再试");
                    }

                    @Override
                    public void onResponse(String response) {
                        mErrorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
                        BaseRequestsModel model = JSON.parseObject(response, BaseRequestsModel.class);
                        LogUtil.i(TAG, "获取商品分类：" + model.getData());
                        if (model != null && model.getResult() == 0) {
                            Dates = JSON.parseArray(model.getData(), GoshopingModel.class);

                            for (int i = 0; i < Dates.size(); i++) {
                                tabLayout.addTab(tabLayout.newTab().setText(Dates.get(i).getName()));

                            }
                            for (GoshopingModel bean : Dates) {
                                Fragment f = PageFragment.newInstance(bean);
                                listFragment.add(f);

                            }
                            adapter.notifyDataSetChanged();
                            tabLayout.setupWithViewPager(viewPager);
                            viewPager.setOffscreenPageLimit(Dates.size());
                        } else {
                            ToastUtil.showShort(GoshoppingActivity.this, NetRequestUtil.requestsError(response, "搜索结果"));

                        }


                    }
                });
    }




    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    @Override
    public void onRVItemClick(ViewGroup viewGroup, View view, int i) {


    }
    class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        private Context context;

        public MyFragmentPagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
        }

        @Override
        public Fragment getItem(int position) {
            return listFragment.get(position);
        }

        @Override
        public int getCount() {
            if (Dates == null)
                return 0;
            else
                return Dates.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return Dates.get(position).getName();
        }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            this.finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

